package com.PMSys.Servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.PMSys.Demain.User;
import com.PMSys.Service.UserService;

public class LoginServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	UserService userService = new UserService();
	/**
	 * Constructor of the object.
	 */
	public LoginServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		
		//����request�е�����
		String loginState=request.getParameter("LoginState");
		
		//��¼��֤
		if(loginState!=null && loginState!=""){
			doLoginCheck(request, response);
		}
	}
	
	
	/**
	 * ��¼��֤
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void doLoginCheck(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		//����http���˻�������
		String account=request.getParameter("username");
		String password=request.getParameter("pwd");
		//������������
		User paramUser=new User();
		paramUser.setUsername(account);
		
		paramUser.setPassword(password);
		
		//��֤�û���Ϣ�Ƿ�Ϸ���staff=���ض���paramUser=����
		User user = userService.checkUser(paramUser);
		if(user==null){
			//��ʾ�û���������Ϣ����,���ص�¼����
			System.out.println("�����˻��������");
			response.getWriter().print("<script>alert('�û������������������');location.href='"+
					request.getContextPath()+"/EquipMMSysLogin.jsp'</script>");
		}else{
			//�ѵ�¼�û���Ϣ��ŵ�session��
			HttpSession session=request.getSession();
			//����sessionʱ��
			session.setMaxInactiveInterval(60*60);
			//���û����������ŵ�session��
			session.setAttribute("user", user);
			
			request.getRequestDispatcher("/.jsp").forward(request, response);
			}
			
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
